﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Apps.Security.ViewModels;

namespace Bzure.Apps.Security.Views
{
    public class ListRole : PageView<ListRoleViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }

        string Script2(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var url =\"{0}\";", url);
            sb.AppendFormat("var notificationYes = \"<!--{0}-->\";", NotificationData.YES);
            sb.Append("   var Del = function (id, trid) {");
            sb.Append(" if (confirm(\"你确定要删除该角色吗？\")) {");
            sb.Append("var deleteurl = url + \"/\" + id;");
            sb.Append("  $.get(deleteurl, null, function(result) { ");
            sb.Append(" if (contains(result, notificationYes, true)) {");
            sb.Append("  $(\"#\" + trid).remove();");
            sb.Append("}");
            sb.Append("$(\"#content_box_messages\").html(result);");
            sb.Append(" $(\".close\").click(");
            sb.Append("function () {");
            sb.Append(" $(this).fadeTo(400, 0, function () {");
            sb.Append("$(this).slideUp(400);");
            sb.Append("});  return false;});");
            sb.Append("     });    }  };");
            return sb.ToString();
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery.validate.min.js" })),
                script.attr(type: MimeType.text.javascript)[Script2(Model.Url)],
                h2[Model.Title],
                p.attr(id: "page-intro")[" 列表系统中所有的安全角色，您可以删除这些角色，但请谨慎，因为关联的角色授权及用户角色也将删除。"],
                div.attr(@class: "clear"),
                div.attr(@class: "content-box")
                [
                     div.attr(@class: "content-box-header")
                      [
                            h3["角色列表"],
                            ul.attr(@class: "content-box-tabs")
                            [
                               li[a.attr(href: "#tab1", @class: "default-tab")["所有"]]
                            ],
                            div.attr(@class: "clear")
                      ],
                     div.attr(@class: "content-box-content")
                      [
                          div.attr(@class: "tab-content default-tab", id: "tab1")
                          [
                                div.attr(id: "content_box_messages"),
                                table.attr(id: "RoleList_Table")
                                [
                                     thead.attr(id: "RoleList_Table_Thead")
                                     [
                                        tr[th["角色名"], th["角色描述"], th["操作"]]
                                     ],
                                     tbody.attr(id: "RoleList_Table_Tbody")
                                     [
                                        ForEach(this.Model.Roles, (item, c) => c
                                            [
                                                   tr.attr(@class: "tablerow rolelist", id: "tr_" + item.ID)
                                                   [
                                                      th[item.Name],
                                                      td[item.RoleDescription],
                                                      td[a.attr(href: "#", onclick: "Del('" + item.ID + "','tr_" + item.ID + "');")["删除"]]
                                                   ]
                                            ])
                                     ]
                                ]
                          ]
                      ]
                ]
            ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
